package com.tt.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.tt.entity.User;
import com.tt.service.UserserviceImpl;

@Controller
public class UserController {

	@Resource
	private UserserviceImpl uImpl;
	@RequestMapping(value="adduser",method=RequestMethod.POST)
	public Object addUser(User user,HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		user.setStatus(0);
		boolean addUser = uImpl.addUser(user);
		if (addUser) {
			out.print("<script language='javascript'>alert('注册成功');window.location.href='userlogin.jsp'</script>");
		}
		out.print("<script language='javascript'>alert('注册失败');window.location.href='register.jsp'</script>");
		return out;
	}
	@RequestMapping(value="/LoginUser",method=RequestMethod.GET)
	public Object login(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String email=request.getParameter("email");
		String password=request.getParameter("password");
//		System.out.println(email+" "+password);
		User user =this.uImpl.getloginUser(email, password);
		if (user!=null) {
		session.setAttribute("user", user);
//		System.out.println(user.getEmail());
			//request.setAttribute("message1", "账号或者密码不正常！");
			//return "/userlogin.jsp";	
		out.print("<script language='javascript'>alert('登录成功');window.location.href='Index.jsp'</script>");
		}
		out.print("<script language='javascript'>alert('登录失败');window.location.href='userlogin.jsp'</script>");
		return out;
	}
	@RequestMapping(value="UpdateUser",method=RequestMethod.POST)
	public Object updateUser(User user,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		boolean updateUser = uImpl.updateUser(user);
		if (updateUser) {
			out.print("<script language='javascript'>alert('修改成功');window.location.href='Index.jsp'</script>");
		}
		else {
			out.print("<script language='javascript'>alert('修改失败');window.location.href='Index.jsp'</script>");
		}
		return out;
		
	}
	@RequestMapping("/toUpdateUser")
	public String toUpdate(HttpServletRequest request,Model model){
		User user = (User) request.getSession().getAttribute("user");
		Integer id = user.getId();
		User user2 = uImpl.toupdate(id);
		model.addAttribute("user", user2);
		return "/userupdate.jsp";	
	}
	@RequestMapping("toUpgrade")
	public String toUpgrade(HttpServletRequest request,Model model){
		User user = (User) request.getSession().getAttribute("user");
		Integer id = user.getId();
		User user2 = uImpl.toupdate(id);
		model.addAttribute("user", user2);
		return "/upgrade.jsp";
	}
	@RequestMapping(value="doupgrade",method=RequestMethod.POST)
	public Object upgrade(User user,@RequestParam("file") MultipartFile file,HttpServletRequest request,HttpServletResponse response)throws IOException{
		User user2 = uImpl.toupdate(user.getId());
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		if (file!=null) {
			String path=request.getServletContext().getRealPath("img");
			File file2=new File(path, file.getOriginalFilename());
			file.transferTo(file2);
			user2.setImg(file.getOriginalFilename());
			user2.setStatus(1);
			user2.setAddress(user.getAddress());
			user2.setShopname(user.getShopname());
			boolean upgrade = this.uImpl.upgrade(user2);
			if (upgrade) {
				out.print("<script language='javascript'>alert('提交成功');window.location.href='Index.jsp'</script>");
			}
			out.print("<script language='javascript'>alert('提交失败');window.location.href='Index.jsp'</script>");
			return out;
		}
		out.print("<script language='javascript'>alert('提交失败，请上传营业执照');window.location.href='Index.jsp'</script>");
		return out;
	}
	@RequestMapping(value="logout")
	public String logout(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.invalidate();
		return "Index.jsp";
	}
}
